package com.ict.txmsv2.controller.dashboard;

import com.ict.txmsv2.model.MethodDescrition;
import com.ict.txmsv2.model.RespBean;
import com.ict.txmsv2.model.TodoList;
import com.ict.txmsv2.services.ToDoListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yoleen
 * @date 2020/12/16 14:55
 */

@RestController
@RequestMapping("/toDoList")
public class ToDoListController {
    @Autowired
    ToDoListService toDoListService;

    @GetMapping("/")
    @MethodDescrition("根据userId获取todolist")
    public List<TodoList> getTodoLists(int userid){
        return toDoListService.getTodoLists(userid);
    }

    @PostMapping("/")
    @MethodDescrition("add todolist")
    public RespBean addTodo(@RequestBody TodoList todoList){
        if (toDoListService.addTodo(todoList)==1) {
            return RespBean.ok("add success");
        }
        return RespBean.error("add error");
    }

    @PutMapping("/")
    @MethodDescrition("update todolist")
    public RespBean updateTodo(@RequestBody TodoList todoList){
        if (toDoListService.updateTodo(todoList)==1) {
            return RespBean.ok("update success");
        }
        return RespBean.error("update error");
    }

    @PutMapping("/noReturn")
    @MethodDescrition("noReturn")
    public void updateTodoNoReturn(@RequestBody TodoList todoList){
        toDoListService.updateTodo(todoList);
    }

    @DeleteMapping("/{id}")
    @MethodDescrition("delete todolist")
    public RespBean deleteTodo(@PathVariable int id){
        if (toDoListService.deleteTodo(id)==1) {
            return RespBean.ok("delete success");
        }
        return RespBean.error("delete error");
    }
}
